package com.yupi.shanoj.controller;


import com.yupi.shanoj.common.BaseResponse;
import com.yupi.shanoj.common.ErrorCode;
import com.yupi.shanoj.common.ResultUtils;
import com.yupi.shanoj.model.dto.video.VideoAddRequest;
import com.yupi.shanoj.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/uploadVideo")
public class VideoUploadController {

    @Autowired
    private VideoService videoService;

    /**
     * 查询当前视频要上传的分片序号
     * @param hash 视频hash值
     * @return
     */
    @GetMapping("/video/ask-chunk")
    public BaseResponse<Long> askChunk(@RequestParam("hash") String hash){
        return videoService.askChunk(hash);
    }

    /**
     * 上传分片
     * @param chunk
     * @param hash
     * @param index
     * @return
     * @throws IOException
     */
    @PostMapping("/video/upload-chunk")
    public BaseResponse<Long> uploadChunk(@RequestParam("chunk") MultipartFile chunk,
                                          @RequestParam("hash") String hash,
                                          @RequestParam("index") Integer index) throws IOException{
        try{
            return videoService.uploadChunk(chunk, hash, index);
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponse<>(500, null, "上传失败");
        }
    }

    @GetMapping("/cancel-upload")
    public BaseResponse<Long> cancelUpload(@RequestParam("hash") String hash){
        return videoService.cancelUpload(hash);
    }

    private Integer userId;
    private String title;
    private Integer type;
    private Integer auth;
    private Double duration;
    private String mcId;
    private String scId;
    private String tags;
    private String descr;
    private String coverUrl;
    private String videoUrl;
    private Integer status;
    @PostMapping("/video/add")
    public BaseResponse<Long> addVideo(@RequestParam("cover") MultipartFile cover,
                                       @RequestParam("videoHash") String videoHash,
                                       @RequestParam("userId") Integer userId,
                                       @RequestParam("title") String title,
                                       @RequestParam("type") Integer type,
                                       @RequestParam("auth") Integer auth,
                                       @RequestParam("duration") Double duration,
                                       @RequestParam("mcId") String mcId,
                                       @RequestParam("scId") String scId,
                                       @RequestParam("tags") String tags,
                                       @RequestParam("descr") String descr){
        VideoAddRequest videoAddRequest = new VideoAddRequest(videoHash,userId,title,type,auth,duration,mcId,scId,tags,descr,null,null,null);

        try{
            return videoService.addVideo(videoAddRequest, cover);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "上传失败");
        }
    }

}
